package com.xlf.book.util;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xie'lin'feng
 * @description
 * @date 2020-07-04-17:05
 * @sesc JDBC工具类
 */
public class DButil {
    /**
     * 打开数据库
     * @return
     * @throws Exception
     */
    public static Connection open(){
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bookmanage?characterEncoding=utf-8","root","");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 关闭数据库
     * @param rs
     * @param ps
     * @param conn
     */
    public static void close(ResultSet rs, PreparedStatement ps,Connection conn){
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (ps != null) {
                ps.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增删改
     * @param sql
     * @param prarm
     * @return
     */
    public static boolean update(String sql,Object [] prarm){
        //增删改两件套
        Connection conn = open();
        PreparedStatement ps = null;
        try{
            //获取执行对象
            ps = conn.prepareStatement(sql);
            //给占位符赋值
            if (prarm!=null){
                for(int i = 0;i<prarm.length;i++){
                    ps.setObject(i+1,prarm[i]);
                }
            }
            //执行sql语句
            return  ps.executeUpdate()>0;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close(null,ps,conn);
        }
        return false;
    }

    /**
     * 解析ResultSet结构集
     * @param rs
     * @param cls
     * @return List<?>
     */
    public static List<?> infoRs(ResultSet rs,Class cls){
        //返回的数据集合
        List<Object> list = new ArrayList<>();
        try {
            while (rs.next()){
                //通过类名,得到对应的实例化对象
                Object obj = cls.newInstance();
                //取出obj的所有属性Field属性字段
                Field[] fields = cls.getDeclaredFields();
                //循环给每一个属性赋值
                for (Field field : fields){
                    //设置属性的操作权限
                    field.setAccessible(true);
                    //给属性赋值
                    field.set(obj,rs.getObject(field.getName()));
                }
                //添加到集合
                list.add(obj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
